package com.wanghaixin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanghaixin.dao.ArticleMapper;
import com.wanghaixin.dto.ArticleDTO;
import com.wanghaixin.dto.DataGridResult;
import com.wanghaixin.dto.QueryDTO;
import com.wanghaixin.pojo.Article;
import com.wanghaixin.pojo.ArticleExample;
import com.wanghaixin.service.ArticleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import java.util.List;

/**
 * @Auther: 王海新
 * @Date: 2020/10/30 17:39
 * @Description: 资讯管理服务层
 */
@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;


    @Override
    public int addArticle(ArticleDTO articleDTO) {
        //因为articleDTO是页面上的实体类，其中的收藏和点赞是boolean类型
        //在数据库里是btye类，所以需要传递数据库的实体类
        Article article = new Article();
        //利用工具将数据封装
        BeanUtils.copyProperties(articleDTO,article);

        //判断是否点击收藏和点赞
        Boolean allowUp = articleDTO.getAllowUp();
        if (allowUp != null && allowUp) {
            article.setAllowUp((byte)1);
        }else {
            article.setAllowUp((byte)0);
        }
        Boolean allowFav = articleDTO.getAllowFav();
        if (allowFav != null && allowFav ) {
            article.setAllowFav((byte)1);
        }else {
            article.setAllowFav((byte)0);
        }

        int i = articleMapper.insertSelective(article);
        return i;
    }

    @Override
    public int delArt(Long id) {
        int i = articleMapper.deleteByPrimaryKey(id);
        return i;
    }

    @Override
    public int updateArticle(ArticleDTO articleDTO) {
        Article article = new Article();
        BeanUtils.copyProperties(articleDTO,article);
        //判断是否点击收藏和点赞
        Boolean allowUp = articleDTO.getAllowUp();
        if (allowUp != null && allowUp) {
            article.setAllowUp((byte)1);
        }else {
            article.setAllowUp((byte)0);
        }
        Boolean allowFav = articleDTO.getAllowFav();
        if (allowFav != null && allowFav ) {
            article.setAllowFav((byte)1);
        }else {
            article.setAllowFav((byte)0);
        }
        int update = articleMapper.updateByPrimaryKeySelective(article);
        return update;
    }

    @Override
    public Article findByID(Long id) {
        Article article = articleMapper.selectByPrimaryKey(id);
        return article;
    }

    @Override
    public DataGridResult findByPage(QueryDTO queryDTO) {
        //4.将分页数据放到PageHelper中
        PageHelper.offsetPage(queryDTO.getOffset(),queryDTO.getLimit());
        //2.创建selectByExamp的参数
        ArticleExample articleExample = new ArticleExample();
        //3.判断是否设置排序
        String sort = queryDTO.getSort();
        if (!StringUtils.isEmpty(sort)) {
            articleExample.setOrderByClause("id");
        }
        //1,调用mapper层selectByExample方法。
        // 然后点进去查看参数。创建参数类
        List<Article> articles = articleMapper.selectByExample(articleExample);
        //5.用pageinfo获取总数数据，用DataGridResult封装数据
        PageInfo<Article> info = new PageInfo<>(articles);
        long total = info.getTotal();
        DataGridResult dataGridResult = new DataGridResult(total,articles);
        return dataGridResult;
    }
}
